package com.ryder.subject.domain.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ryder.subject.common.enums.CategoryTypeEnum;
import com.ryder.subject.common.enums.IsDeleteFlagEnum;
import com.ryder.subject.domain.convert.SubjectLabelConvert;
import com.ryder.subject.domain.entity.SubjectLabelBO;
import com.ryder.subject.domain.service.SubjectLabelDomainService;
import com.ryder.subject.infra.basic.entity.SubjectCategory;
import com.ryder.subject.infra.basic.entity.SubjectLabel;
import com.ryder.subject.infra.basic.entity.SubjectMapping;
import com.ryder.subject.infra.basic.service.SubjectCategoryService;
import com.ryder.subject.infra.basic.service.SubjectLabelService;
import com.ryder.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述: 题目标签表(SubjectLabel)表服务实现类
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0 // 版本号，指明从哪个版本开始
 * 更新日期: 2025/02/05
 */

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Autowired
    private SubjectLabelService subjectLabelService;

    @Autowired
    private SubjectMappingService subjectMappingService;

    @Autowired
    private SubjectCategoryService subjectCategoryService;

    /**
     * 添加标签
     *
     * @param subjectLabelBO 标签BO
     * @return 成功：true，失败：false
     */
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToLabel(subjectLabelBO);
        //设置为未删除
        subjectLabel.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
        int resRow = subjectLabelService.insert(subjectLabel);
        return resRow > 0;
    }

    /**
     * 更新标签
     *
     * @param subjectLabelBO 标签BO
     * @return 成功：true，失败：false
     */
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToLabel(subjectLabelBO);
        int resRow = subjectLabelService.update(subjectLabel);
        return resRow > 0;
    }

    /**
     * 删除标签
     *
     * @param id 标签id
     * @return 成功：true，失败：false
     */
    @Override
    public Boolean delete(Long id) {
        SubjectLabel subjectLabel = new SubjectLabel();
        subjectLabel.setId(id);
        //设置为已删除
        subjectLabel.setIsDeleted(IsDeleteFlagEnum.DELETED.getCode());
        int resRow = subjectLabelService.update(subjectLabel);
        return resRow > 0;
    }

    /**
     * 根据分类id查询标签
     *
     * @param subjectLabelBO 标签BO
     * @return 标签列表
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.bo:{}",
                    JSON.toJSONString(subjectLabelBO));
        }
        //如果当前分类是1级分类，则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if (CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()) {
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> labelList = subjectLabelService.queryByCondtion(subjectLabel);
            List<SubjectLabelBO> resultLabelBoList = SubjectLabelConvert.INSTANCE.convertLabelListToBoList(labelList);
            return resultLabelBoList;
        }


        //主要的条件
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        //结果为空返回一个空且不可修改的集合
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }
        List<Long> labelIdList = mappingList.stream()
                .map(SubjectMapping::getLabelId)
                .toList();
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        LinkedList<SubjectLabelBO> boList = new LinkedList<>();
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(categoryId);
            bo.setSortNum(label.getSortNum());
            boList.add(bo);
        });
        return boList;
    }
}
